package com.bustle.ejb.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "`order`")
public class Order
{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int idOrder;

  @ManyToOne
  @JoinColumn(name = "idAccount")
  private Account account;

  @Enumerated(EnumType.STRING)
  private OrderStatus orderStatus;

  @Temporal(TemporalType.TIMESTAMP)
  private Date creationDate;

  @OneToMany(fetch=FetchType.EAGER)
  private List<Lineitem> lineItems = new ArrayList<Lineitem>();


  public Order()
  {
  }


  public int getIdOrder()
  {
    return idOrder;
  }

  public void setIdOrder(int idOrder)
  {
    this.idOrder = idOrder;
  }

  public Account getAccount()
  {
    return account;
  }

  public void setAccount(Account account)
  {
    this.account = account;
  }

  public OrderStatus getOrderStatus()
  {
    return orderStatus;
  }

  public void setOrderStatus(OrderStatus orderStatus)
  {
    this.orderStatus = orderStatus;
  }

  public Date getCreationDate()
  {
    return creationDate;
  }

  public void setCreationDate(Date creationDate)
  {
    this.creationDate = creationDate;
  }

  public double getTotalPrice() {
	  double total = 0;
	  for (Lineitem li : lineItems){
			total += li.getTotal();
	  }
	  return total;
  }

  public List<Lineitem> getLineItems()
  {
    return lineItems;
  }

  public void setLineItems(List<Lineitem> lineItems)
  {
    this.lineItems = lineItems;
  }


  public enum OrderStatus
  {
    PAID,
    WAITING,
    CANCELLED
  }

}